//
//  BingJianViewController.swift
//  xdjcy
//
//  Created by Henry on 15/12/8.
//  Copyright (c) 2015年 Henry. All rights reserved.
//

import UIKit

class BingJianViewController: UIViewController, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    
    @IBOutlet weak var collectionView: UICollectionView!
    var selectedIndex = -1
    var url = ""
    var tabName = ""
    private let sectionInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

    //课程名称和图片，每一门课程用字典来表示
    let courses = [
        ["name":"我要举报","pic":"woyaojubao.png"],
        ["name":"举报查询","pic":"jubaochaxun.png"],
        ["name":"行贿档案","pic":"xinhuidangan.png"],
        ["name":"法律咨询","pic":"xinxichaxun.png"],
        ["name":"法律咨询查询","pic":"yuyuechaxun.png"],
        ["name":"控告申诉","pic":"lianluopingtai.png"],
        ["name":"律师预约","pic":"lvshiyuyue.png"],
        ["name":"律师预约查询","pic":"lsyuyuechaxun.png"],
        ["name":"联系我们","pic":"contactus.png"],
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self

        self.view.addSubview(collectionView)

//        for v  in self.view.subviews as! Array<UIView>
//        {
//            var tapgesture1  = UITapGestureRecognizer(target: self, action: "jump:")
//            v.userInteractionEnabled = true
//            v.addGestureRecognizer(tapgesture1)
//        }
        
    }
    
    func jump(tap:UITapGestureRecognizer)
    {
        selectedIndex = tap.view!.tag
        switch selectedIndex
        {
        case 1:
            tabName = "举报查询"
            url = JuBaoChaXunUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 2:
            tabName = "我要举报"
            url = WoYaoJuBaoUrl
             self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 3:
            tabName = "行贿档案"
            url = XingHuiDangAnMsgUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 4:
            tabName = "法律咨询"
            url = FaLvZiXunUrl
             self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 5:
            tabName = "法律咨询查询"
            url = FaLvZiXunChaXunUrl
             self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 6:
            tabName = "控告审诉"
            url = KongGaoShenSuUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 7:
            tabName = "律师预约"
            url = LvShiYuYueUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 8:
            tabName = "律师预约查询"
            url = LvShiYuYueChaXunUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 9:
            tabName = "联系我们"
            url = LianXiWoMenUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        default:
            break
        }
       
        println(tap.view?.tag)
        
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1;
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      
        return courses.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectioncell", forIndexPath: indexPath) as! UICollectionViewCell

        var imageTitle = cell.viewWithTag(1) as! UIImageView
        var lblTitle = cell.viewWithTag(2) as! UILabel
        
        imageTitle.image = UIImage(named: courses[indexPath.item]["pic"]!)
        lblTitle.text = courses[indexPath.item]["name"]!
        return cell;
    }
    
//    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
//        return sectionInsets;
//    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        return CGSize(width: collectionView.bounds.width / 3, height: 100)
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        println(indexPath.item)

        switch indexPath.item
        {
        case 0:
            tabName = "我要举报"
            url = WoYaoJuBaoUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 1:
            tabName = "举报查询"
            url = JuBaoChaXunUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 2:
            tabName = "行贿档案"
            url = XingHuiDangAnMsgUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 3:
            tabName = "法律咨询"
            url = FaLvZiXunUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 4:
            tabName = "法律咨询查询"
            url = FaLvZiXunChaXunUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 5:
            tabName = "控告审诉"
            url = KongGaoShenSuUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 6:
            tabName = "律师预约"
            url = LvShiYuYueUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 7:
            tabName = "律师预约查询"
            url = LvShiYuYueChaXunUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        case 8:
            tabName = "联系我们"
            url = LianXiWoMenUrl
            self.performSegueWithIdentifier("jumpToWebView", sender: self)
            break
        default:
            break
        }
    }
    
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var controller=segue.destinationViewController as! UIViewController
        if controller.isKindOfClass(WebViewViewController)
        {
            controller.setValue(url, forKey: "showUrl")
            controller.setValue(tabName, forKey: "tabName")
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}
